package com.rtype.GPSilencer;

import com.rtype.GPSilencer.helpers.ProfileDbAdapter;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;

public class GPSilencer extends ListActivity {

	private ProfileDbAdapter mProfileDbHelper;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mProfileDbHelper = new ProfileDbAdapter(this);
        mProfileDbHelper.open(); 
        fillProfileData();
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.main_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// Handle menu item selection
    	switch (item.getItemId()) {
    	case R.id.new_profile:
    		// Start activity for new profile creation
    		Intent intent = new Intent(this, ProfileDetails.class);
    		startActivity(intent);
    		return true;
    	default:
        	return super.onOptionsItemSelected(item);
    	}
    }
    
    private void fillProfileData() {
        Cursor notesCursor = mProfileDbHelper.fetchAll();
        startManagingCursor(notesCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{ProfileDbAdapter.KEY_NAME};

        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.profile_name};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter profiles = 
            new SimpleCursorAdapter(this, R.layout.profile_row, notesCursor, from, to);
        setListAdapter(profiles);
    }
}